<?php
/**
 * @file actionEdit.php
 * @author LD
 * @package news
 * @desc 编辑文章
 */
global $module, $action, $nid, $page, $module_name;
	
	/* 001.分类 */
	$cats = $dbh->getCategories();
	$cat_options = array();
	
foreach ($cats as $cat){
	if ( $cat['type'] == $module ) 
		$cat_options[ $module_name ][$cat['ID']] = $cat['name'];
}	
$smarty->assign("cat_options", $cat_options);


		/* 002.发布者 */
	$auhs = $dbh->getAuthors();
	$auh_options = array();
	foreach ($auhs as $auh){
		$auh_options[$auh['ID']] = $auh['name'];
	}
$smarty->assign("auh_options", $auh_options);

		/* 003.添加动作 */
if ($action == 'add'){
	$smarty->assign("page_title", "添加文章");
	//$smarty->assign("cat_id", $cats[0]['ID']);
	//$smarty->assign("auh_id", $auhs[0]['ID']);
	if ( $_POST['newsSubmit'] == '添加文章' ){
		$post = array();
		$post['author_id'] = $_POST['newsAuthor'];
		$post['category_id'] = $_POST['newsCats'];
		$post['title'] = trim($_POST['newsTitle']);		
		$post['content'] = $_POST['newsContent'];
		$post['type'] = $module;
		
		$xTag = $_POST['newsTags'];
		$xTag = makeTags($xTag);
		$tags = array( implode(' ',$xTag) );
		
		if ( empty($post['content']) or empty($post['title']) ) {
			$smarty->assign('error', "文章标题 和 内容 不能为空！");		
		}else{
			//var_dump($post);
			$ID = $dbh->insertPost($post);
			if ($ID == 0) $smarty->assign('error', "文章插入失败！");
			else{
				$dbh->insertTags($ID, $tags);
				$smarty->assign('success', "【{$ID}】{$post['title']}");
			}
		}
	}//END 提交表单
		
}//END action add  		/* 003.删除动作 */
elseif ($action == 'edit'){
	$smarty->assign("page_title", "编辑文章");
	if (empty($nid)) $smarty->assign('error', "没有传入 文章 的ID！");
	else{
		$smarty->assign('nid', $nid);
		if ( $_POST['newsSubmit'] == '编辑文章' ){
			$post = array();
			$post['author_id'] = $_POST['newsAuthor'];
			$post['category_id'] = $_POST['newsCats'];
			$post['title'] = trim($_POST['newsTitle']);		
			$post['content'] = $_POST['newsContent'];
			$post['type'] = $module;
			
			$xTag = $_POST['newsTags'];
			$xTag = makeTags($xTag);
			$tags = array( implode(' ',$xTag) );
			
			if ( empty($post['content']) or empty($post['title']) ) {
				$smarty->assign('error', "文章标题 和 内容 不能为空！");		
			}else{
				$dbh->updateNews($nid, 'post', $post);
				$dbh->updateNews($nid, 'tag', $tags);
				$smarty->assign('success', "【{$nid}】{$post['title']}");
			}
		}//END 提交表单
		
		$post = $dbh->getPost($nid);
		$smarty->assign('xTitle', $post['title'] );
		$smarty->assign('xContent', $post['content'] );
		$smarty->assign("cat_id", $post['category_id'] );
		$smarty->assign("auh_id", $post['author_id'] );
		
		$xTag = $dbh->getTag($nid);
		$smarty->assign('xTag', $xTag['name']);
	}
}//END action delete

$smarty->display('news_post.html');
?>